package com.nkdx.ws.response;

import com.nkdx.ws.enums.CodeEnum;
import com.nkdx.ws.enums.StatusEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *
 *  接口统一返回值对象
 * @author ZZW
 * @date 2021/12/23 11:30 上午
 * @param
 * @return
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> implements Serializable {

    /**
     *  请求状态    1 成功    2 失败
     */
    private Integer status;

    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 泛型对象
     */
    private T data;


    public ApiResponse success(T data) {
        this.status = StatusEnum.REQUEST_SUCCESS.getStatus();
        this.code = CodeEnum.REQUEST_SUCCESS.getCode();
        this.msg = CodeEnum.REQUEST_SUCCESS.getMsg();
        this.data = data;
        return this;
    }

    public ApiResponse error(T data) {
        this.status = StatusEnum.REQUEST_ERROR.getStatus();
        this.code = CodeEnum.REQUEST_FAILURE.getCode();
        this.msg = CodeEnum.REQUEST_FAILURE.getMsg();
        this.data = data;
        return this;
    }

    public ApiResponse error(Integer code,String msg,T data) {
        this.status = StatusEnum.REQUEST_ERROR.getStatus();
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public ApiResponse error(CodeEnum codeEnum,T data){
        this.status = StatusEnum.REQUEST_ERROR.getStatus();
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
        this.data = data;
        return this;
    }

}
